﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Model;
using System.Data;

namespace DAL
{
    public class HouseTypeService
    {
        public static HouseType AddHouseType(HouseType hType)
        {
            string sql = string.Format("insert into HouseType(Id,Name) values({0},N'{1}');",hType.Id, hType.Name);
            DBHelper.GetScalar(sql);
            return GetHouseTypeById(hType.Id);
        }

        public static HouseType GetHouseTypeById(int id)
        {
            string sql = string.Format("select * from HouseType where Id={0}", id);
            return GetHouseTypeBySql(sql);
        }

        public static List<HouseType> GetAllHouseTypes()
        {
            string sql = string.Format("select * from HouseType");
            return GetHouseTypesBySql(sql);
        }

        private static HouseType GetHouseTypeBySql(string sql)
        {
            List<HouseType> list = GetHouseTypesBySql(sql);
            if (list.Count() > 0)
                return list[0];
            return null;
        }

        private static List<HouseType> GetHouseTypesBySql(string sql)
        {
            List<HouseType> list = new List<HouseType>();
            DataTable dt = DBHelper.GetDataSet(sql);
            foreach (DataRow row in dt.Rows)
            {
                HouseType hType = new HouseType();
                hType.Id = (int)row["Id"];
                hType.Name = (string)row["Name"];
                list.Add(hType);
            }
            return list;
        }
    }
}
